package com.jsnj33.pdaproject.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jsnj33.pdaproject.R;
import com.jsnj33.pdaproject.bean.MainBean;

import java.util.List;

/**
 * @author 33oa
 * @date 2018/11/29
 */
public abstract class FormHorizontalAdapter extends RecyclerView.Adapter<FormHorizontalAdapter.ViewHolder> {

    private Context mContext;
    private List<MainBean> list;

    public FormHorizontalAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_form_hor, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final MainBean mainBean = list.get(position);
        holder.tv.setText(list.get(position).getName());
        if (mainBean.isChick()) {
            holder.tv.setBackgroundResource(R.color.white);
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.main_blue));
        }else {
            holder.tv.setBackgroundResource(R.color.main_blue);
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.white));
        }

        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i=0; i<list.size(); i++){
                    list.get(i).setChick(false);
                }
                list.get(position).setChick(true);
                itemChick(mainBean.getAddress());

                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    protected abstract void itemChick(String address);

    public void setList(List<MainBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        ViewHolder(View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv);
        }
    }
}
